package com.intellagric.service.iftmonitor.Impl.ift_ctrl_equipment;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.IftCtrlEquipmentButtonMapper;
import com.intellagric.mapper.IftCtrlEquipmentChannelMapper;
import com.intellagric.mapper.IftCtrlEquipmentMapper;
import com.intellagric.module.iftmonitor.CtrlEquipmentChannelVo;
import com.intellagric.pojo.*;
import com.intellagric.service.iftmonitor.ift_ctrl_equipment.CtrlEquipmentChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/12/15
 * @DESC： 控制设备通道服务
 */
@Service
public class CtrlEquipmentChannelServiceImpl implements CtrlEquipmentChannelService {

    @Autowired
    private IftCtrlEquipmentChannelMapper ctrlEquipmentChannelMapper;

    @Autowired
    private IftCtrlEquipmentMapper ctrlEquipmentMapper;

    @Autowired
    private IftCtrlEquipmentButtonMapper ctrlEquipmentButtonMapper;

    /**
     * 添加控制设备通道和控制设备
     * @param ctrlEquipmentChannel
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addCtrlEquipmentChannel(IftCtrlEquipmentChannel ctrlEquipmentChannel) {
        //根据控制通道封装控制设备信息
        IftCtrlEquipment ctrlEquipment = new IftCtrlEquipment();
        ctrlEquipment.setId(ctrlEquipmentChannel.getCtrlEquipmentChannel());
        ctrlEquipment.setNumber(ctrlEquipmentChannel.getCtrlChannelNo() + "");
        ctrlEquipment.setIsService("0");//默认关闭
        ctrlEquipment.setName(ctrlEquipmentChannel.getCtrlChannelName());
        ctrlEquipment.setTypeId(ctrlEquipmentChannel.getCtrlChannelTypeId());
        ctrlEquipment.setEquipmentId(ctrlEquipmentChannel.getEquipmentId());
        ctrlEquipment.setSceneId(ctrlEquipmentChannel.getSceneId());
        ctrlEquipment.setCreateDate(new Date());
        ctrlEquipment.setUpdateDate(new Date());

        if (ctrlEquipmentChannelMapper.insertSelective(ctrlEquipmentChannel) == 1
                && ctrlEquipmentMapper.insertSelective(ctrlEquipment) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除控制设备通道，同时删除控制设备与对应的按钮信息
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteCtrlEquipmentChannel(String id) {
        //删除控制设备
        IftCtrlEquipmentChannel ctrlEquipmentChannel = ctrlEquipmentChannelMapper.selectByPrimaryKey(id);
        //删除按钮
        IftCtrlEquipmentButtonExample ctrlEquipmentButtonExample = new IftCtrlEquipmentButtonExample();
        ctrlEquipmentButtonExample.createCriteria().andRelIdEqualTo(id);
        ctrlEquipmentButtonMapper.deleteByExample(ctrlEquipmentButtonExample);

        if (ctrlEquipmentChannelMapper.deleteByPrimaryKey(id) == 1 &&
                ctrlEquipmentMapper.deleteByPrimaryKey(ctrlEquipmentChannel.getCtrlEquipmentChannel()) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改控制设备通道
     * @param ctrlEquipmentChannel
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editCtrlEquipmentChannel(IftCtrlEquipmentChannel ctrlEquipmentChannel) {
        if (ctrlEquipmentChannelMapper.updateByPrimaryKeySelective(ctrlEquipmentChannel) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询控制设备通道
     * @param id
     * @return ResponseMessage
     */
    @Override
    public IftCtrlEquipmentChannel getCtrlEquipmentChannelById(String id) {
        return ctrlEquipmentChannelMapper.selectByPrimaryKey(id);
    }

    /**
     * 多条件查询控制设备通道
     * @param ctrlEquipmentChannelVo
     * @return ResponseMessage
     */
    @Override
    public LayuiDataGridResult getCtrlEquipmentChannelListByVo(CtrlEquipmentChannelVo ctrlEquipmentChannelVo) {
        IftCtrlEquipmentChannelExample ctrlEquipmentChannelExample = new IftCtrlEquipmentChannelExample();
        ctrlEquipmentChannelExample.createCriteria().andEquipmentIdEqualTo(ctrlEquipmentChannelVo.getEquipmentId());
        List<IftCtrlEquipmentChannel> ctrlEquipmentChannelList = ctrlEquipmentChannelMapper.selectByExample(ctrlEquipmentChannelExample);
        return LayuiDataGridResult.success().add(ctrlEquipmentChannelList,ctrlEquipmentChannelList.size());
    }

    /**
     * 查询控制设备通道列表
     * @return ResponseMessage
     */
    @Override
    public LayuiDataGridResult getCtrlEquipmentChannelList() {
        IftCtrlEquipmentChannelExample ctrlEquipmentChannelExample = new IftCtrlEquipmentChannelExample();
        ctrlEquipmentChannelExample.createCriteria().andRelIdIsNotNull();
        List<IftCtrlEquipmentChannel> ctrlEquipmentChannelList = ctrlEquipmentChannelMapper.selectByExample(ctrlEquipmentChannelExample);
        return LayuiDataGridResult.success().add(ctrlEquipmentChannelList,ctrlEquipmentChannelList.size());
    }
}
